import * as winston from 'winston';
import { Config } from '../config/Config';

export class Logger {
    private logger: winston.Logger;

    constructor() {
        const logLevel = Config.get('LOG_LEVEL') || 'info';
        const logFilePath = Config.get('LOG_FILE_PATH') || 'logs/server.log';

        this.logger = winston.createLogger({
            level: logLevel,
            format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.json()
            ),
            transports: [
                // 控制台输出
                new winston.transports.Console({
                    format: winston.format.combine(
                        winston.format.colorize(),
                        winston.format.simple()
                    )
                }),
                // 文件输出
                new winston.transports.File({
                    filename: logFilePath,
                    maxsize: 5242880, // 5MB
                    maxFiles: 5,
                    tailable: true
                })
            ]
        });
    }

    public info(message: string, meta?: any): void {
        this.logger.info(message, meta);
    }

    public error(message: string, error?: any): void {
        this.logger.error(message, {
            error: error instanceof Error ? {
                message: error.message,
                stack: error.stack,
                name: error.name
            } : error
        });
    }

    public warn(message: string, meta?: any): void {
        this.logger.warn(message, meta);
    }

    public debug(message: string, meta?: any): void {
        this.logger.debug(message, meta);
    }

    public silly(message: string, meta?: any): void {
        this.logger.silly(message, meta);
    }
} 